const userTable=document.querySelector('.userTable')
const changeUserBox =document.querySelector('.changeUserBox')
const changeUserForm =document.querySelector('.changeUserForm')
const emailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
const pwdReg=/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,16}$/
const changeUserName=document.querySelector('#changeUserName')
const changeSex=document.querySelector('#changeSex')
const changeAge=document.querySelector('#changeAge')
const changeAddress=document.querySelector('#changeAddress')
const changeEmail=document.querySelector('#changeEmail')
const changeisManager=document.querySelector('#changeisManager')
userTable.addEventListener('click', (e) => {
    if (e.target.classList.contains('delUserBtn')) {
        e.preventDefault();
        let userId=e.target.dataset.userid
        customConfirm('确定要删除该用户吗', (result) => {
            if (result) {
                $.ajax({
                    url: '/weibo/user/deleteUser/'+userId,
                    success: function (result) {
                        showCustomAlert('删除成功',true)
                    },
                    error: function (error) {
                        showCustomAlert('删除失败',false)
                    },
                });
                setTimeout(() => {
                    location.reload()
                }, 1000);
            }
        });
    }
    if (e.target.classList.contains('banUserBtn')) {
        e.preventDefault();
        let userId=e.target.dataset.userid
        customConfirm('确定要禁言该用户吗', (result) => {
            if (result) {
                $.ajax({
                    url: '/weibo/user/banUserToComment/'+userId,
                    success: function (result) {
                        showCustomAlert('禁言成功',true)
                    },
                    error: function (error) {
                        showCustomAlert('禁言失败',false)
                    },
                });
                setTimeout(() => {
                    location.reload()
                }, 1000);
            }
        });
    }
    if (e.target.classList.contains('cancelBanBtn')) {
        e.preventDefault();
        let userId=e.target.dataset.userid
        customConfirm('确定要解禁该用户吗', (result) => {
            if (result) {
                console.log(userId)
                $.ajax({
                    url: '/weibo/user/notBanUserToComment/'+userId,
                    success: function (result) {
                        showCustomAlert('解禁成功',true)
                    },
                    error: function (error) {
                        showCustomAlert('解禁失败',false)
                    },
                });
                setTimeout(() => {
                    location.reload()
                }, 1000);
            }
        });
    }
    if (e.target.classList.contains('changeInfoBtn')) {
        e.preventDefault();
        let userId=e.target.dataset.userid
        customConfirm('确定要修改该用户的信息吗', (result) => {
            if (result) {
                changeUserBox.classList.remove('hidden')
                let selectedRow = e.target.closest('tr');
                changeUserForm.username.value = selectedRow.cells[0].innerText;
                changeUserForm.sex.value = selectedRow.cells[1].innerText;
                changeUserForm.age.value = selectedRow.cells[2].innerText;
                changeUserForm.address.value = selectedRow.cells[4].innerText;
                changeUserForm.email.value = selectedRow.cells[6].innerText;
                changeUserForm.manager.value=(selectedRow.cells[7].innerText==='用户'?0:1)
                changeUserForm.addEventListener('submit',(e)=>{
                    e.preventDefault()
                    let changeUserNameValue=setText(changeUserForm.username.value)
                    let changeAgeValue=setText(changeUserForm.age.value)
                    let changeEmailValue=setText(changeUserForm.email.value)
                    let changeSexValue=setText(changeUserForm.sex.value)
                    let changeAddressValue=setText(changeUserForm.address.value)
                    let changeisManagerValue=setText(changeUserForm.manager.value)
                    if(changeUserNameValue.length>15){
                        showCustomAlert('昵称长度应为1~15个字符',false)
                        changeUserName.classList.add('warning')
                    }else if (changeAgeValue<0||changeAgeValue>120||!(changeAgeValue%1 === 0)){
                        showCustomAlert('年龄必须为0~120间的整数',false)
                        changeAge.classList.add('warning')
                    }else if(changeEmailValue===''){
                        showCustomAlert('邮箱不能为空!',false)
                        changeEmail.classList.add('warning')
                    }else if(!emailReg.test(changeEmailValue)){
                        showCustomAlert('邮箱格式不正确!',false)
                        changeEmail.classList.add('warning')
                    }else{
                        changeUserName.classList.remove('warning')
                        changeAge.classList.remove('warning')
                        changeEmail.classList.remove('warning')
                        $.ajax({
                            url:'/weibo/user/updateUser',
                            data:{
                                username:changeUserNameValue,
                                id:userId,
                                address:changeAddressValue,
                                age:changeAgeValue,
                                email:changeEmailValue,
                                manager:changeisManagerValue,
                                sex:changeSexValue
                            },
                            success: function (result) {
                                showCustomAlert('修改成功',true)
                            },
                            error: function (error) {
                                showCustomAlert('修改失败',false)
                            },
                        });
                        setTimeout(() => {
                            location.reload()
                        }, 1000);
                    }

                })
            }
        });
    }
})

const addUserBtn=document.querySelector('.addUserBtn')
const addUserBox=document.querySelector('.addUserBox')
addUserBtn.addEventListener('click',()=>{
    addUserBox.classList.remove('hidden')
})

const addUserForm=document.querySelector('.addUserForm')
const cancelBtn=document.querySelector('.cancelBtn')
const cancelAdd=document.querySelector('.cancelAdd')
const cancelChange=document.querySelector('.cancelChange')
const cancelChangeBtn=document.querySelector('.cancelChangeBtn')
cancelChange.addEventListener('click',(e)=>{
    customConfirm('关闭之后已修改的信息将恢复',(result)=>{
        if (result){
            changeUserName.classList.remove('warning')
            changeAge.classList.remove('warning')
            changeEmail.classList.remove('warning')
            changeUserForm.reset()
            changeUserBox.classList.add('hidden')
        }
    })
})
cancelChangeBtn.addEventListener('click',(e)=>{
    customConfirm('关闭之后已修改的信息将恢复',(result)=>{
        if (result){
            changeUserName.classList.remove('warning')
            changeAge.classList.remove('warning')
            changeEmail.classList.remove('warning')
            changeUserForm.reset()
            changeUserBox.classList.add('hidden')
        }
    })
})
cancelBtn.addEventListener('click',(e)=>{
    customConfirm('关闭之后已编辑的信息将清空',(result)=>{
        if (result){
            addUserName.classList.remove('warning')
            addAge.classList.remove('warning')
            addEmail.classList.remove('warning')
            addpwd.classList.remove('warning')
            addUserForm.reset()
            addUserBox.classList.add('hidden')
        }
    })
})
cancelAdd.addEventListener('click',(e)=>{
    customConfirm('关闭之后已编辑的信息将清空',(result)=>{
        if (result){
            addUserName.classList.remove('warning')
            addAge.classList.remove('warning')
            addEmail.classList.remove('warning')
            addpwd.classList.remove('warning')
            addUserForm.reset()
            addUserBox.classList.add('hidden')
        }
    })
})

const addUserName=document.querySelector('#addUserName')
const addAge=document.querySelector('#addAge')
const addEmail=document.querySelector('#addEmail')
const addpwd=document.querySelector('#addpwd')


addUserForm.addEventListener('submit',(e)=>{
    e.preventDefault()
    let addUserNameValue=setText(addUserForm.username.value)
    let addAgeValue=setText(addUserForm.age.value)
    let addEmailValue=setText(addUserForm.email.value)
    let addPwdValue=setText(addUserForm.password.value)
    if(addUserNameValue.length>15){
        showCustomAlert('昵称长度应为1~15个字符',false)
        addUserName.classList.add('warning')
    }else if (addAgeValue<0||addAgeValue>120||!(addAgeValue%1 === 0)){
        showCustomAlert('年龄必须为0~120间的整数',false)
        addAge.classList.add('warning')
    }else if(addEmailValue===''){
        showCustomAlert('邮箱不能为空!',false)
        addEmail.classList.add('warning')
    }else if(!emailReg.test(addEmailValue)){
        showCustomAlert('邮箱格式不正确!',false)
        addEmail.classList.add('warning')
    }else if(addPwdValue===''){
        showCustomAlert('密码不能为空!',false)
        addpwd.classList.add('warning')
    }else if(!pwdReg.test(addPwdValue)){
        showCustomAlert('密码应为8-16位，包含大小写字母和数字!',false)
        addpwd.classList.add('warning')
    }else{
        addUserName.classList.remove('warning')
        addAge.classList.remove('warning')
        addEmail.classList.remove('warning')
        addpwd.classList.remove('warning')
        addUserForm.submit()
    }

})
const seaUserForm=document.querySelector('.seaUserForm')
seaUserForm.addEventListener('submit',function (e){
    e.preventDefault()
    seaUserForm.condition.value=setText(seaUserForm.condition.value)
    seaUserForm.submit()
})
